package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityAvatarDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.bean.StkResBean;

public class AvatarDetailActivity extends BaseAc<ActivityAvatarDetailBinding> {

    public static StkResBean avatarBean;

    @Override
    protected int onCreate() {
        return R.layout.activity_avatar_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAvatarDetailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivCollect.setOnClickListener(this);
        mDataBinding.ivSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCollect://收藏
                List<StkResBean> listSp = AppSpUtils.getAvatarCollectList();
                if (avatarBean.isSelected()) {
                    avatarBean.setSelected(false);
                    listSp.remove(avatarBean);
                    AppSpUtils.saveAvatarCollectList(listSp);
                } else {
                    if (listSp != null && listSp.size() > 0) {
                        listSp.add(avatarBean);
                        AppSpUtils.saveAvatarCollectList(listSp);
                    } else {
                        List<StkResBean> listSave = new ArrayList<>();
                        listSave.add(avatarBean);
                        AppSpUtils.saveAvatarCollectList(listSave);
                    }
                    avatarBean.setSelected(true);
                }
                isCollect();
                break;
            case R.id.ivSave://保存
                showDialog(getString(R.string.save_ing));
                Downloader.newTask(mContext)
                        .url(avatarBean.getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".jpg")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                dismissDialog();
                                ToastUtils.showShort(getString(R.string.save_to_album));
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                dismissDialog();
                                ToastUtils.showShort(R.string.save_failure);
                            }
                        });
                break;
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(avatarBean.getUrl()).into(mDataBinding.ivAvatarImg);
//        int width = mDataBinding.ivAvatarImg.getMeasuredWidth();
//        ViewGroup.LayoutParams layoutParams = mDataBinding.ivAvatarImg.getLayoutParams();
//        layoutParams.height = width;
//        mDataBinding.ivAvatarImg.setLayoutParams(layoutParams);
        isCollect();
    }

    private void isCollect() {
        if (avatarBean.isSelected()) {
            mDataBinding.ivCollect.setImageResource(R.drawable.an_sc2);
        } else {
            mDataBinding.ivCollect.setImageResource(R.drawable.an_sc1);
        }
    }

}